package com.bocomm.tracevisualization.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bocomm.tracevisualization.api.entity.TranslateManagement;
import com.bocomm.tracevisualization.api.result.CodeMessage;
import com.bocomm.tracevisualization.api.result.Result;
import com.bocomm.tracevisualization.api.service.ITranslateManagementService;
import com.bocomm.tracevisualization.api.vo.req.QueryTranslateManagementReq;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * <p>
 * 终端翻译管理 前端控制器
 * </p>
 *
 * @author cxp
 * @since 2024-03-23
 */
@RestController
@RequestMapping("/api/translateManagement")
public class TranslateManagementController {


    @Autowired
    ITranslateManagementService translateManagementService;
    /**
     * 终端管理列表查询
     * @return 终端管理列表
     */
    @PostMapping("/query")
    public Result<IPage<TranslateManagement>> query(@RequestBody QueryTranslateManagementReq queryTranslateManagementReq){
        IPage<TranslateManagement> page = new Page<>();

        QueryWrapper<TranslateManagement> queryWrapper = new QueryWrapper<>();
        if(Objects.nonNull(queryTranslateManagementReq)){
            page = new Page<>(queryTranslateManagementReq.getPageNum(),queryTranslateManagementReq.getSize());

            queryWrapper.lambda()
                    .like(StringUtils.hasText(queryTranslateManagementReq.getTerminalIp()),TranslateManagement::getTerminalIp,queryTranslateManagementReq.getTerminalIp())
                    .or().like(StringUtils.hasText(queryTranslateManagementReq.getTerminalIp()),TranslateManagement::getAlias,queryTranslateManagementReq.getTerminalIp());
        }


        IPage<TranslateManagement> pageRecord = translateManagementService.page(page, queryWrapper);

        return Result.success(pageRecord);
    }

    /**
     * 终端管理列表查询
     * @return 终端管理列表
     */
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody TranslateManagement terminalManagement) {
        if(!Objects.nonNull(terminalManagement)){
            return Result.fail(CodeMessage.VALIDATE_ERROR);
        }
        translateManagementService.saveOrUpdateAndChack(terminalManagement);
        return Result.success();
    }


    /**
     * 终端管理删除操作
     * @return 终端管理列表
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody TranslateManagement translateManagement)  {
        if(!Objects.nonNull(translateManagement) || Objects.isNull(translateManagement.getId())){
            return Result.fail(CodeMessage.VALIDATE_ERROR);
        }
        translateManagementService.removeById(translateManagement.getId());
        return Result.success();
    }
}
